Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports Aricie.DNN.Services
Imports Lucene.Net.Documents
Imports Engage.Dnn.Publish

Namespace Aricie.DNN.Modules.LuceneSearch.ModuleProviders


    Public Class EngagePublishProvider
        Implements ILuceneSearchableUpgrade
        Implements ILuceneFieldGlossary

#Region "Fields"
        Public Class Fields
            Public Const Thematique As String = "Thematique"
            Public Const RelatedCategory2 As String = "RelatedCategory2"
            Public Const RelatedCategory3 As String = "RelatedCategory3"
        End Class
#End Region

#Region "Implement ILuceneSearchableUpgrade"
        Public Sub UpgradeSearchItem(ByVal portalId As Integer, ByRef searchItem As LuceneSearchItemInfo) _
            Implements ILuceneSearchableUpgrade.UpgradeSearchItem

            Dim factory As FieldFactory = FieldFactory.Instance
            Dim itemId As Integer = Integer.Parse(searchItem.SearchKey.Substring(searchItem.SearchKey.LastIndexOf("-"c) + 1))
            If itemId <> -1 Then
                Dim myArticle As Article = Article.GetArticle(itemId, portalId)
                searchItem.ItemId = itemId
                Dim parentCatId As Integer = myArticle.GetParentCategoryId
                Dim category As String = GetParentCategoryNameByArticle(itemId, portalId)
                If Not String.IsNullOrEmpty(category) Then
                    searchItem.Category = category
                    searchItem.CategoryId = parentCatId
                End If
                Dim thematique As String = GetRelatedCategoryNameByArticleId(itemId, portalId, 0)
                If Not String.IsNullOrEmpty(thematique) Then
                    searchItem.AdditionalFields.Add(factory.CreateField(Fields.Thematique, thematique))
                End If
                Dim related2 As String = GetRelatedCategoryNameByArticleId(itemId, portalId, 1)
                If Not String.IsNullOrEmpty(related2) Then
                    searchItem.AdditionalFields.Add(factory.CreateField(Fields.RelatedCategory2, related2))
                End If
                Dim related3 As String = GetRelatedCategoryNameByArticleId(itemId, portalId, 2)
                If Not String.IsNullOrEmpty(related3) Then
                    searchItem.AdditionalFields.Add(factory.CreateField(Fields.RelatedCategory3, related3))
                End If
                Dim tags() As String = GetTagNames(itemId, portalId).Split(",")
                For Each objTag As String In tags
                    searchItem.AdditionalFields.Add(factory.CreateField(LuceneGeneralField.Tags, objTag))
                Next
            End If


        End Sub
#End Region

#Region "Implement ILuceneFieldGlossary"
        Public Function GetFieldDefinitions() As IList(Of FieldDefinition) _
            Implements ILuceneFieldGlossary.GetFieldDefinitions
            Dim toReturn As New List(Of FieldDefinition)
            toReturn.Add(New FieldDefinition(Fields.Thematique, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO, True, 3))
            toReturn.Add(New FieldDefinition(Fields.RelatedCategory2, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO, True, 2))
            toReturn.Add(New FieldDefinition(Fields.RelatedCategory3, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO, True, 1))
            Return toReturn
        End Function
#End Region

#Region "Methods"
        Public Function GetArticleTitle(ByVal itemId As Integer, ByVal portalId As Integer) As String

            Dim objArticle As Article = Article.GetArticle(itemId, portalId)
            Dim toReturn As String = objArticle.Name
            Return toReturn
        End Function

        Public Function GetParentCategoryNameByArticle(ByVal itemId As Integer, ByVal portalId As Integer) As String
            Dim toReturn As String = ""
            Dim objArticle As Article = Article.GetArticle(itemId, portalId)
            If objArticle IsNot Nothing Then
                Dim list As ArrayList = ItemRelationship.GetItemRelationships(itemId, objArticle.ItemVersionId, Util.RelationshipType.ItemToParentCategory.GetId, True, objArticle.PortalId)
                If list.Count > 0 Then
                    Dim objItemRelationship As ItemRelationship = list(0)
                    Dim objCategory As Category = Category.GetCategory(objItemRelationship.ParentItemId, objArticle.PortalId)
                    toReturn = objCategory.Name
                End If
            End If
            Return toReturn
        End Function

        Public Function GetCategoryName(ByVal categoryId As Integer, ByVal portalId As Integer) As String
            Dim toReturn As String = ""
            Dim objCategory As Category = Category.GetCategory(categoryId, portalId)
            If objCategory IsNot Nothing Then
                toReturn = objCategory.Name
            End If
            Return toReturn
        End Function

        Public Function GetRelatedCategoryNameByArticleId(ByVal itemId As Integer, ByVal portalId As Integer, ByVal categoryIndex As Integer) As String
            Dim toReturn As String = ""
            Dim objArticle As Article = Article.GetArticle(itemId, portalId)
            If objArticle IsNot Nothing Then
                Dim list As ArrayList = ItemRelationship.GetItemRelationships(itemId, objArticle.ItemVersionId, Util.RelationshipType.ItemToRelatedCategory.GetId, True, objArticle.PortalId)
                If list.Count > categoryIndex Then
                    Dim objItemRelationship As ItemRelationship = list(categoryIndex)
                    Dim objCategory As Category = Category.GetCategory(objItemRelationship.ParentItemId, objArticle.PortalId)
                    toReturn = objCategory.Name
                End If
            End If
            Return toReturn
        End Function

        Public Function GetTagNames(ByVal itemId As Integer, ByVal portalId As Integer) As String
            Dim toReturn As String = ""
            Dim objArticle As Article = Article.GetArticle(itemId, portalId)
            If ((Not objArticle Is Nothing) AndAlso objArticle.Tags.Count < 1) Then
                Dim itemTags As ArrayList = ItemTag.GetItemTags(objArticle.ItemVersionId, objArticle.PortalId)
                If itemTags.Count > 0 Then
                    For Each objItemTag As ItemTag In itemTags
                        toReturn = Tag.GetTag(objItemTag.TagId, objArticle.PortalId).Name & ","
                    Next
                    toReturn = toReturn.Remove(toReturn.Length - 1)
                End If
            End If
            Return toReturn
        End Function
#End Region

    End Class
End Namespace

